package cn.cyl.org.controller;

import cn.cyl.basic.annotation.PreAuthorize;
import cn.cyl.basic.util.AjaxResult;
import cn.cyl.basic.util.PageList;
import cn.cyl.org.domain.Department;
import cn.cyl.org.domain.Employee;
import cn.cyl.org.query.EmployeeQuery;
import cn.cyl.org.service.DepartmentService;
import cn.cyl.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门接口类",description = "一些CRUD操作")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;
    /**
     * 接口：查所有
     * @return
     */
    @ApiOperation(value = "接口：查所有")
    @GetMapping
    @PreAuthorize(name = "员工查询所有",sn = "employee:getAll")
    public List<Employee> findAll(){
       return employeeService.loadAll();
    }

    /**
     * 接口：查一个
     * @param id
     * @return
     */
    @ApiOperation(value = "接口：查一个")
    @GetMapping("/{id}")
    @PreAuthorize(name = "员工查询一个",sn = "employee:getOne")
    public Employee findOne(@PathVariable("id") Long id){
        return employeeService.loadOne(id);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @ApiOperation(value = "接口：删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "员工删除一个",sn = "employee:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param employee
     * @return
     */
    @ApiOperation(value = "接口：添加或修改")
    @PutMapping
    @PreAuthorize(name = "员工保存",sn = "employee:save")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);
                Long department_id = employee.getDepartment_id();
                System.out.println(department_id);

            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：高级和分页
     * @param employeeQuery
     * @return
     */

    @ApiOperation(value = "接口：高级和分页")
    @PostMapping
    @PreAuthorize(name = "员工的分页查询",sn = "employee:list")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "接口：批量删除")
    @PatchMapping
    @PreAuthorize(name = "员工的批量删除",sn = "employee:patchDel")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除操作失败");
        }
    }
    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

    /**
     * 接口：点击接单显示该店的店长
     * @param shopId
     * @return
     */

    @GetMapping("/list/{shopId}")
    public List<Employee> queryByShopId(@PathVariable("shopId") Long shopId){
        return employeeService.queryByShopId(shopId);

    }
}
